/*
 * @(#)XfplotInputDialog.java        2003/01/01
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */


package xfuzzy.xfplot.view;

import xfuzzy.lang.*;
import xfuzzy.util.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Di�logo para fijar los valores de las variables de entrada
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfplotInputDialog extends JDialog implements ActionListener {
	
	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603055L;
	
	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                              //
	//----------------------------------------------------------------------------//
	
	/**
	 * Referencia a la aplicaci�n
	 */
	private Xfplot xfplot;
	
	/**
	 * Campos para introducir los valores
	 */
	private XTextForm text[];
	
	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//
	
	/**
	 * Constructor
	 */
	public XfplotInputDialog(Xfplot xfplot) {
		super(xfplot,"Xfplot",true);
		this.xfplot = xfplot;
		
		Variable input[] = xfplot.getInputVariables();
		double value[] = xfplot.getInputValues();
		text = new XTextForm[input.length];
		for(int i=0; i<text.length; i++) {
			text[i] = new XTextForm(input[i].getName());
			text[i].setText(""+value[i]);
			text[i].setFieldWidth(200);
		}
		XTextForm.setWidth(text);
		
		String lb[] = { "Set", "Cancel" };
		XCommandForm form = new XCommandForm(lb,lb,this);
		form.setCommandWidth(100);
		
		Container content = getContentPane();
		content.setLayout(new BoxLayout(content,BoxLayout.Y_AXIS));
		content.add(new XLabel("Fixed Values for Input Variables"));
		for(int i=0; i<text.length; i++) content.add(text[i]);
		content.add(form);
		
		Point loc = xfplot.getLocationOnScreen();
		loc.x += 40; loc.y += 200;
		this.setLocation(loc);
		pack();
	}
	
 	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//
	
	/**
	 * Interfaz ActionListener
	 */
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if(command.equals("Set")) set();
		else if(command.equals("Cancel")) setVisible(false);
	}
	
	//----------------------------------------------------------------------------//
	//                             M�TODOS PRIVADOS                               //
	//----------------------------------------------------------------------------//
	
	/**
	 * Seleciona los valor iniciales de las variables
	 */
	private void set() {
		boolean good = true;
		double value[] = new double[text.length];
		for(int i=0; i<text.length; i++) {
			try { value[i] = Double.parseDouble(text[i].getText()); }
			catch (NumberFormatException e) { good = false; text[i].setText(""); }
		}
		if(good && xfplot.setInputValues(value)) setVisible(false);
		else Toolkit.getDefaultToolkit().beep(); 
	}
}

